package common;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Util {
	 
    public static String docToString(Node node) {
        try {
            Source source = new DOMSource(node);
            StringWriter stringWriter = new StringWriter();
            Result result = new StreamResult(stringWriter);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            transformer.transform(source, result);
            return stringWriter.getBuffer().toString();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static Document stringToDoc(String xmlSource) throws ParserConfigurationException, SAXException, IOException{
        
    	DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbfactory.newDocumentBuilder();
        
        return db.parse(new InputSource(new StringReader(xmlSource)));
    }
    
    public static String getTagValue(String tag, Element element) {
        return element.getElementsByTagName(tag).item(0).getTextContent();
    }
    
	public static boolean isNumeric(String str) {
		
		Pattern p = Pattern.compile("((-|\\+)?[0-9]+(\\.[0-9]+)?)+");
		Matcher m = p.matcher(str);
		boolean b = m.matches();

		return b;
	}
	
	public static int RandomInteger() {
		//note a single Random object is reused here
		Random randomGenerator = new Random();
		int randomInt = randomGenerator.nextInt(10);
		return randomInt;
	}

	/** Generate random integers in a certain range. */
	public static int RandomRange() {

		int START = 1;
		int END = 10;
		Random random = new Random();

		//get the range, casting to long to avoid overflow problems
		long range = (long)END - (long)START + 1;
		// compute a fraction of the range, 0 <= frac < range
		long fraction = (long)(range * random.nextDouble());
		int randomNumber =  (int)(fraction + START);
		return randomNumber;
	}

}